package org.keyyond.ms.commonservice.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.keyyond.ms.commonservice.model.SDBDDepartment;
import org.keyyond.ms.commonservice.model.SDBDRole;
import org.keyyond.ms.commonservice.service.SDBDDeptService;
import org.keyyond.ms.commonservice.util.CommonBeanUtils;
import org.keyyond.ms.core.model.MsCommonMessageLogVo;
import org.keyyond.ms.core.util.CommonBeanUtil;
import org.keyyond.ms.core.util.Constant;
import org.keyyond.ms.core.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import net.sf.json.JSONArray;

@RestController
@RequestMapping("/sdbdDept")
@RefreshScope
public class SDBDDeptController {
	
	private static Logger logger = Logger.getLogger(SDBDDeptController.class);
	
	@Autowired
	private CommonBeanUtils beanUtils;
	
	@Autowired
	private SDBDDeptService deptService;
	
	@RequestMapping(value = "/selectAll", method = RequestMethod.POST)
	public String selectAll(HttpServletRequest req) {
		MsCommonMessageLogVo logVo = new MsCommonMessageLogVo();
		String traceId = req.getHeader(Constant.CONST_REQUEST_HEADER_TRACEID);
		logVo.setTraceid(traceId);
		logVo.setLogtype(org.keyyond.ms.commonservice.util.Constant.CONST_LOG_TYPE_COMMON_SERVICE);
		logVo.setSendtype(org.keyyond.ms.commonservice.util.Constant.CONST_SEND_TYPE_COMMON_SERVICE_FAILE);
		logVo.setContent(JsonUtil.toJson(req.getParameterMap()));
		JSONArray dataArray = new JSONArray();
		try {
			List<SDBDDepartment> deptList = deptService.selectAll();
			for (SDBDDepartment d : deptList) {
				dataArray.add(JsonUtil.beanToJson(d));
			}
			logVo.setSendtype(org.keyyond.ms.commonservice.util.Constant.CONST_SEND_TYPE_COMMON_SERVICE_SUCCESS);
			logVo.setReturnmessage(dataArray.toString());
			beanUtils.addMessageLog(logVo);
			return CommonBeanUtil.returnResult(true, dataArray);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return Constant.FALL_BACK_INFO;
		}
	}

}
